﻿using AbpApplication;
using AbpEntityFrameworkcore.DbContext;
using AspNetCoreRateLimit;
using Microsoft.EntityFrameworkCore;
using Microsoft.OpenApi.Models;
using Volo.Abp;
using Volo.Abp.AspNetCore.ExceptionHandling;
using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp.Autofac;
using Volo.Abp.Modularity;

namespace AbpWebapi
{
    [DependsOn(
        typeof(AbpAspNetCoreMvcModule),
        typeof(AbpAutofacModule),
        typeof(AbpApplicationModule)
        
        
        )]
    public class ApiModule : AbpModule
    {
        
            public override void ConfigureServices(ServiceConfigurationContext context)
            {

            context.Services.AddSwaggerGen(c =>
                {
                    var scheme = new OpenApiSecurityScheme()
                    {
                        Description = "Authorization header. \r\nExample: 'Bearer 12345abcdef'",
                        Reference = new OpenApiReference
                        {
                            Type = ReferenceType.SecurityScheme,
                            Id = "Authorization"
                        },
                        Scheme = "oauth2",
                        Name = "Authorization",
                        In = ParameterLocation.Header,
                        Type = SecuritySchemeType.ApiKey,
                    };
                    c.AddSecurityDefinition("Authorization", scheme);
                    var requirement = new OpenApiSecurityRequirement();
                    requirement[scheme] = new List<string>();
                    c.AddSecurityRequirement(requirement);
                });
            context.Services.Configure<AbpExceptionHandlingOptions>(options =>
            {
                options.SendExceptionsDetailsToClients = true;// true 异常处理，发送到客户端
            });
            context.Services.AddControllers();
            context.Services.AddEndpointsApiExplorer();
            }//对应的startuo中的ConfigureServices方法
            public override void OnApplicationInitialization(ApplicationInitializationContext context)
            {
                var Environment = context.GetEnvironment();
                var app = (WebApplication)context.GetApplicationBuilder();
                if (Environment.IsDevelopment())
                {                 
                }              
                app.UseSwagger();
                app.UseSwaggerUI();
                app.UseRouting();
                app.UseHttpsRedirection();
            app.UseAuthentication();//认证中间件  注意顺序
            app.UseAuthorization();//授权中间件
            app.MapControllers();
            }
        

    }
}
